package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author: hea7en
 * @Date: 2024/5/13 10:43 AM
 * @Description: 通用控制层
 */
@Slf4j
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用模块接口")
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> uploadFile(MultipartFile file){
        // 上传到本地
        return uploadLocal(file);

        // 上传到阿里云
//    return uploadAliOss(file);
    }

    /**
     * 上传文件到阿里云OSS
     * @param file
     * @return
     */
    private Result<String> uploadAliOss(MultipartFile file) {
        // 获取到原始文件名
        String fileName = file.getOriginalFilename();
        // 截取到文件扩展名
        String extension = fileName.substring(fileName.lastIndexOf("."));
        // 拼接新文件名
        String objectName = UUID.randomUUID() + extension;
        try {
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败：{}", e.getMessage());
            e.printStackTrace();
        }
        return Result.success(MessageConstant.UPLOAD_FAILED);
    }

    /**
     * 上传文件到本地
     * @param file
     * @return
     */
    private Result<String> uploadLocal(MultipartFile file) {
        // 获取到原始文件名
        String fileName = file.getOriginalFilename();
        // 截取到文件扩展名
        String extension = fileName.substring(fileName.lastIndexOf("."));
        // 拼接新文件名
        String objectName = UUID.randomUUID() + extension;
//    objectName = file.getOriginalFilename();
        // 文件存储路径
//    String basePath = getClass().getResource("/").getPath();
        String basePath = this.getClass().getClassLoader().getResource("").getPath();
        // 本地源路径
        String currPath = basePath.split("target")[0]+"src/main/resources/static/imgs/"+objectName;
        log.info("本地源路径：{}", currPath);
        // 本地类路径
        String classPath = basePath+"static/imgs/"+objectName;
        log.info("本地类路径：{}", classPath);
        try (FileOutputStream outPutStream = new FileOutputStream(currPath)){
            // 设置本地源文件存储目录
            File currFile = new File(currPath);
            if (!currFile.exists()) {
                currFile.mkdirs();
            }
            // 将文件上传到本地源路径
            outPutStream.write(file.getBytes());

            // 设置本地类文件存储目录
            File currClassFile = new File(classPath);
            if (!currClassFile.exists()) {
                currClassFile.mkdirs();
            }
            // 将文件上传到本地类路径
            file.transferTo(currClassFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 返回本地文件访问路径，需配置静态资源映射
        return Result.success("http://localhost:8080/static/imgs/" + objectName);
    }
}
